﻿using Nebular.ExpenseManagement.Infrastructure;
using Nebular.ExpenseManagement;
using System;
using System.Collections.Generic;
using System.Data.Linq;
using System.Text;

namespace Nebular.ExpenseManagement.Model
{
    public class BatchJob : IBatchJob
    {
        #region Constants_statevalues

        // a newly created job waiting to be processed.
        public const string newjob = "new";
        // a job in process. this means some work instances i running this job
        public const string running = "running";
        //jobs done. a succesful run
        public const string done = "done";
        //jobs been paused and should no be procced by work instances.
        public const string paused = "paused";
        //job has failed. equivelant to done but marked as failed.
        public const string failed = "failed";

        #endregion


        #region ibatchjob members

        public string JobID
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public IBatchJob[] GetBatchJobs()
        {
            throw new NotImplementedException();
        }

        public Guid CreateBatchJob(string JobName)
        {

            Guid newGuid = Guid.NewGuid();

            
            using (BatchDataContext batchContext = new BatchDataContext("Data Source=msdbb4.surftown.dk;Initial Catalog=nebular_invoices;Persist Security Info=True;User ID=Nebular_admin;Password=maserati"))
            {


                try
                {
                    batch_JobQueue job = new batch_JobQueue();

                    job.Id = newGuid;
                    job.JobName = JobName;
                    job.State = BatchJob.newjob;
                    DateTime date = System.DateTime.Now;
                    job.Created = date;
                    job.Modified = date;


                    batchContext.batch_JobQueues.InsertOnSubmit(job);

                    batchContext.SubmitChanges();
                }
                catch (Exception ex)
                {
                    System.Diagnostics.Debug.WriteLine(ex.ToString());
                    throw ex;
                }


            }

            return newGuid;
            
        }

        public void UpdateBatchJob(string state, Guid jobId)
        {
            throw new NotImplementedException();
        }

        #endregion

    }
}
